﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDauGia.Domain.Abstract;
//using WebDauGia.Domain.Entities;
using WebDauGia.Domain.Models;

namespace WebDauGia.Controllers
{
    public class AdminHomeController : Controller
    {
        //
        // GET: /AdminHome/
        private WebRepository repository;
        public AdminHomeController(WebRepository repo)
        {
            repository = repo;
        }
        public ActionResult Index()
        {
            return View();
        }
        
        public ActionResult DanhMucSanPham()
        {
            return View(repository.DanhMucSanPhams);
        }
        public ViewResult CreateDanhMucSanPham() {
            return View("EditDanhMucSanPham", new DanhMucSanPham());
        }
        public ViewResult EditDanhMucSanPham(int id) {
            
            
            DanhMucSanPham dmsp = repository.DanhMucSanPhams
                .FirstOrDefault(x => x.id == id);
            return View(dmsp);
        }
        [HttpPost]
        public ActionResult EditDanhMucSanPham(DanhMucSanPham dmsp) {
            if (ModelState.IsValid)
            {
                repository.SaveDMSP(dmsp);
                TempData["message"] = string.Format("{0} đã được lưu", dmsp.ten);
                return RedirectToAction("DanhMucSanPham");
            }
            else { 
                //có lỗi xảy ra - something wrong with the data values
                return View(dmsp);
            }
        }

        [HttpPost]
        public ActionResult DeleteDMSP(int id) {
            WebDauGia.Domain.Models.DanhMucSanPham deletedDMSP = repository.DeleteDMSP(id);
            if (deletedDMSP != null) {
                TempData["message"] = string.Format("{0} đã được xoá", deletedDMSP.ten);
            } return RedirectToAction("DanhMucSanPham");
        }
    }
}
